import dayjs from 'dayjs'
import Schema from 'validate'
const formInfo = new Schema({
  houseId: {
    type: String,
    required: true,
    message: '请选择报修房屋~~~'
  },
  repairItemId: {
    type: String,
    required: true,
    message: '请选择维修项目~~~'
  },
  mobile: {
    type: String,
    required: true,
    match: /^1[3456789]\d{9}$/,
    message: '请输入手机号~~~'
  },
  appointment: {
    type: String,
    required: true,
    message: '请选择预约时间~~~'
  },
  description: {
    type: String,
    required: true,
    message: '请填写问题描述，1~200字符~~~'
  },
  attachment: {
    type: Array,
    length: { min: 0, max: 9 },
    each: {
      id: { type: String, required: true },
      url: { type: String, required: true }
    },
    message: '请传入图片~~~'
  },
})
Page({
  data: {
    currentDate: new Date().getTime(),
    mindate: new Date().getTime(),
    maxdate: +dayjs().add(3, 'day'),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    attachment: [], // 图片列表
    houseId: '',//报修房屋的id
    houseName: '',// 报修房屋名字
    repairItemId: '',//维修项目的id
    repairItemName: '',// 维修项目的name
    appointment: '',//选择日期
    mobile: '15555937360',//手机号
    description: '6666666666',//描述信息
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async goList() {
    // wx.reLaunch({
    //   url: '/repair_pkg/pages/list/index',
    // })
    const { houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    const data = { houseId, repairItemId, mobile, appointment, description, attachment }
    const errors = formInfo.validate(data)
    if (errors.length) {
      wx.utils.toast(errors[0].message)
      return
    }
    console.log(data);
    const res = await wx.http({
      url: "/repair",
      method: 'post',
      data
    })
    console.log(res);
    //提示信息
    wx.showToast({
      title: "报修成功",
      icon: 'success'
    })
    // 页面跳转
    setTimeout(() => {
      wx.reLaunch({
        url: '/repair_pkg/pages/list/index',
      })
    }, 1500);
  },
  onLoad() {
    // 获取报修房屋
    this.getRepair()
    //查询维修项目-选项
    this.getRepairItem()
  },
  // 获取报修房屋
  async getRepair() {
    const res = await wx.http({
      url: "/house"
    })
    console.log(res);
    this.setData({ houseList: res.data })
  },
  // vant组件内置方法 bind:select
  onHousepage(e) {
    console.log(e);
    const { id, name } = e.detail
    this.setData({ houseId: id, houseName: name })
  },
  //查询维修项目-选项
  async getRepairItem() {
    const res = await wx.http({
      url: "/repairItem"
    })
    console.log(res);
    this.setData({ repairItem: res.data })
  },
  onHouseRepairItem(e) {
    const { id, name } = e.detail
    this.setData({
      repairItemId: id,
      repairItemName: name
    })
  },
  onFormat(e) {
    console.log(e);
    this.setData({
      currentDate: e.detail,
      appointment: dayjs(e.detail).format('YYYY-MM-DD'),
      dateLayerVisible: false
    })
  },
  //预约时间的取消
  claoseFormat() {
    this.setData({
      dateLayerVisible: false
    })
  },
  // 图片上传
  getImage(e) {
    console.log('eeeeee -----> ', e.detail.file.url);
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: e.detail.file.url,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().token}`
      },
      success: (res) => {
        console.log('res -----> ', res);
        const data = JSON.parse(res.data)
        this.setData({
          attachment: [...this.data.attachment, data.data]
        })
      }
    })
  },
  onDelete(e) {
    console.log('e -----> ', e);
    wx.showModal({
      title: '温馨提示',
      content: `您确定要删除第${e.detail.index + 1}张图片吗？`,
      success: ({ confirm }) => {
        if (confirm) {
          const { attachment } = this.data
          attachment.splice(e.detail.index, 1)
          this.setData({ attachment })
        }
      }
    })
  }
})
